package crack.ch1;

/**
 * @author Pavel Belevich
 *         Date: 6/16/12
 *         Time: 10:28 PM
 */
public class Problem1 {

    /*
    time complexity O(n)
    space complexity O(1)
     */
    public static boolean isUniqueChars2(String str) {
        if (str.length() > 256) return false;

        boolean[] char_set = new boolean[256];
        
        for (int i = 0 ; i < str.length() ; i++) {
            int val = str.charAt(i);
            if (char_set[val]) {
                return false;
            }
            char_set[val] = true;
        }
        
        return true;
    }

    public static boolean isUniqueChars(String str) {
        if (str.length() > 256) return false;

        int checker = 0;
        for (int i = 0 ; i < str.length() ; i++) {
            int val = str.charAt(i) - 'a';
            if ((checker & (1 << val)) > 0) {
                return false;
            }
            checker |= (1 << val);
        }
        return true;
    }

    public static void main(String[] args) {
        String s1 = "dfgelrkgnmelkgtrnrjktbh";
        String s2 = "abcdefghi";
        System.out.println(isUniqueChars2(s1));
        System.out.println(isUniqueChars2(s2));
        System.out.println(isUniqueChars(s1));
        System.out.println(isUniqueChars(s2));
    }

}
